module Assign1(KEY,CLOCK_50,SW,LEDG,LEDR,HEX0,HEX1,HEX2,HEX3);
	input [3:0] KEY;
	input CLOCK_50;
	input [0:0] SW;
	output [7:0] LEDG;
	output [9:0] LEDR;
	output [6:0] HEX0, HEX1, HEX2, HEX3;

	reg [7:0] cntSec; //(0-59)
	reg [3:0] cntMinLo; // (0-9)
	reg [2:0] cntMinHi; // (1-6)
	reg [3:0] cntHourLo; // (0-9)
	reg [1:0] cntHourHi; // (0-2)
	reg [25:0] counter; //for timing
	reg [0:0] lightOn; //1 on, 0 off
	reg [0:0] k0state, k1state, k2state, k3state;
		
		always @(posedge CLOCK_50) begin
		
			
			if (SW[0]==1 && k0state==1 && KEY[0]==0) begin
				if(cntMinLo == 4'd0) begin //logic for treating corner cases with decrementing mins
					cntMinLo <= 4'd9;
					if (cntMinHi == 3'd0) begin
						cntMinHi<=3'd5;
						if (cntHourLo==4'd0 && cntHourHi==2'd0) begin
							cntHourHi<=2'd2;
							cntHourLo<=4'd3;
						end else if (cntHourLo==4'd0) begin		
							cntHourLo<=4'd9;
							cntHourHi<=cntHourHi-2'd1;
						end else begin
						cntHourLo<=cntHourLo-4'd1;
						end
					end else begin
						cntMinHi<=cntMinHi - 3'd1;
					end		
				end else begin
					cntMinLo <= cntMinLo - 4'd1;
				end
				cntSec<=8'd0;
				k0state=0;
			end else if (SW[0]==1 && KEY[0]==1) begin
				k0state=1;
			end
			
			
			if (SW[0]==1 && k1state==1 && KEY[1]==0) begin
				if(cntMinLo == 4'd9) begin  //logic for treating corner cases with incrementing mins
					cntMinLo <= 4'd0;
					if (cntMinHi == 3'd5) begin
						cntMinHi<=3'd0;
						if (cntHourLo==4'd3 && cntHourHi==2'd2) begin
							cntHourHi<=2'd0;
							cntHourLo<=4'd0;
						end else if (cntHourLo==4'd9) begin
							cntHourLo<=4'd0;
							cntHourHi<=cntHourHi+2'd1;
						end else begin
							cntHourLo<=cntHourLo+4'd1;
						end
					end else begin
						cntMinHi<=cntMinHi + 3'd1;
					end		
				end else begin
						cntMinLo <= cntMinLo + 4'd1;
				end
				cntSec<=8'd0;
				k1state=0;
			end else if (SW[0]==1 && KEY[1]==1) begin
				k1state=1;
			end
			
			
			
			if (SW[0]==1 && k2state==1 && KEY[2]==0) begin
				if (cntHourLo==4'd0 && cntHourHi==2'd0) begin //logic for treating corner cases with decrementing hour
					cntHourHi<=2'd2;
					cntHourLo<=4'd3;
				end else if (cntHourLo==4'd0) begin
					cntHourLo<=4'd9;
					cntHourHi<=cntHourHi-2'd1;
				end else begin
					cntHourLo<=cntHourLo-4'd1;
				end
				k2state=0;
			end else if (SW[0]==1 && KEY[2]==1) begin
				k2state=1;
			end
			if (SW[0]==1 && k3state==1 && KEY[3]==0) begin
				if (cntHourLo==4'd3 && cntHourHi==2'd2) begin
					cntHourHi<=2'd0;
					cntHourLo<=4'd0;
				end else if (cntHourLo==4'd9) begin
					cntHourLo<=4'd0;
					cntHourHi<=cntHourHi+2'd1;
				end else begin
					cntHourLo<=cntHourLo+4'd1;
				end
				k3state=0;
			end else if (SW[0]==1 && KEY[3]==1) begin
				k3state=1;
			end
			
			
			
			if (counter < 26'd5000000 && counter!=26'd25000000) begin //50 000 000 ticks/sec
				counter <= counter + 26'd1;
			end else if (counter == 26'd25000000 && SW[0]==1) begin //.5 seconds
				if (lightOn==0) begin
					lightOn=1;
				end else begin
					lightOn=0;
				end	
				counter <= counter + 26'd1;
			end else begin
				counter=26'd0;
				if(cntSec == 8'd60) begin
					cntSec <= 8'd0;
					if(cntMinLo == 4'd9) begin
						cntMinLo <= 4'd0;
						if (cntMinHi == 3'd5) begin
							cntMinHi<=3'd0;
							if (cntHourLo==4'd3 && cntHourHi==2'd2) begin
								cntHourHi<=2'd0;
								cntHourLo<=4'd0;
							end else if (cntHourLo==4'd9) begin
								cntHourLo<=4'd0;
								cntHourHi<=cntHourHi+2'd1;
							end else begin
								cntHourLo<=cntHourLo+4'd1;
							end
						end else begin
							cntMinHi<=cntMinHi + 3'd1;
						end		
					end else begin
						cntMinLo <= cntMinLo + 4'd1;
					end
				end else begin
					cntSec <= cntSec + 8'd1;
				end
			end
			if (SW[0]==0) begin //1 is pressed
				lightOn=0;
			end 
		end	
	redBlink rawr(LEDR, lightOn);	
	dispSec secon(LEDG, cntSec);
	dispHex minutLo(HEX0, cntMinLo);
	dispHex minutHi(HEX1, cntMinHi);
	dispHex hourLo(HEX2, cntHourLo);
	dispHex hourHi(HEX3, cntHourHi);
endmodule

module redBlink(led, num);
	output[9:0] led;
	input[0:0] num;
		assign led=
			(num==0) ? 10'b0000000000: //1 is led on
			(num==1) ? 10'b1111111111:
						10'b0000000000;
endmodule

module dispHex(sseg, num);
	output[6:0] sseg;
	input [7:0] num;
	assign sseg=
		(num == 4'd0) ? 7'b1000000 :
		(num == 4'd1) ? 7'b1111001 :
		(num == 4'd2) ? 7'b0100100 :
		(num == 4'd3) ? 7'b0110000 :
		(num == 4'd4) ? 7'b0011001 :
		(num == 4'd5) ? 7'b0010010 :
		(num == 4'd6) ? 7'b0000010 :
		(num == 4'd7) ? 7'b1111000 :
		(num == 4'd8) ? 7'b0000000 :
		(num == 4'd9) ? 7'b0010000 :
					    7'b0110000 ;
endmodule

module dispSec(led,num);
	output[7:0] led;
	input [7:0] num;
	assign led=num;
endmodule